// pages/mine/mine.js
var api = require("../../api.js"),
  r = "",
  app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    priceData:[],
    pageIndex:1,
    loading: false, //第一次加载，设置true  
    loadingComplete:false, //把“没有数据”设为false，隐藏  
    isFromSearch: true, 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     r =this;
     r.initData(); //初始化数据

  },
  initData:function(){
    var t= new Object();
    t.openId = getApp().globalData.wxUserInfo.openId,
    t.type= r.data.currentTab,
    t.pageIndex= 1,t.pageSize=10;
     wx.request({
       url:api.mine.orderOfMine,
       data:t,
       success:function(res){
         if(res.statusCode==200){
           if(res.data.code==200){
            var n = res.data.data;
            if(n.length!=0){
              var checkStatus='';
              n.length<10?checkStatus=false:true; 
             setTimeout(()=>{
              r.setData({
                priceData: n,
                loading:checkStatus,
                loadingComplete:false,
                hidden:false
              });
             },500)
            }else{
              setTimeout(()=>{
                r.setData({
                  loadingComplete:false,
                  loading:false,
                  hidden:true
                })
              },500)
             
            } 



            //  if( r.data.currentTab==0){
            //    var dataList = r.data.priceData.concat(n);
            //    r.data.pageIndex++;
            //    r.setData({
            //      priceData:dataList
            //     })
            //  }else{
            //   var dataList = r.data.priceData.concat(n);
            //   r.data.pageIndex++;
            //   r.setData({
            //     priceData1:dataList
            //    })
            //  }
           }
          
           
         }
       }
     })
  },
  // 加载数据
  loading(){
    var t= new Object();
    var dataList= [];
    t.openId = getApp().globalData.wxUserInfo.openId,
    t.type= r.data.currentTab,
    t.pageIndex= r.data.pageIndex,t.pageSize=10;
    wx.request({
      url: api.mine.orderOfMine,
      data:t,
      success: function(res) {
        if(res.statusCode==200){
          if(res.data.code==200){
            var n = res.data.data;
            if(n.length>0){
              r.data.isFromSearch ? priceData = n : priceData=r.data.data.concat(n)  
              setTimeout(()=>{
              r.setData({
                priceData: dataList,
                loading:true,
              });
              },500)
            }else{
              setTimeout(()=>{
                r.setData({
                  loadingComplete:true,
                  loading:false,
                })
              },500)
              
            }
        
          
          }
        }
      }
    });
  },
 /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let _that = this
    if(_that.data.loading && !_that.data.loadingComplete){
        _that.setData({
          pageIndex:_that.data.pageIndex+1,
          isFromSearch:false
         });
         _that.loading();
    };
  },
  //点击切换 
  clickTab: function(e) {
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        priceData:[],
        pageIndex:1,
        loading: false, 
        loadingComplete:false, 
        isFromSearch: true, 
        hidden:false
      });
      r.initData();
    }
  },
  gotail:function(e){
    console.log(e)
    var o = e.currentTarget.dataset;
    wx.navigateTo({
      url: "../paydetail/paydetail?orderid=" + o.orderid
    });
  },
  pay:function(e){
    var id= e.currentTarget.dataset.ordid;
    var t = new Object();
    t.ordId = id,t.openId =  getApp().globalData.wxUserInfo.openId;
    wx.request({
      url:api.pay.toContinuePay,
      data:t,
      success:function(res){
         console.log(res)
         if(res.statusCode==200){
           if(res.data.code==200){
             var data = res.data.data;
            wx.requestPayment({
              timeStamp: data.timeStamp.toString(),
              nonceStr: data.nonceStr,
              package: data.package,
              signType: "MD5",
              paySign: data.paySign,
              success: function(res) {
                console.log("支付成功");
              },
              fail: function(res) {
                console.log("支付失败");
                return;
              },
              complete: function(res) {
                console.log("支付完成");
                console.log(res);
                if (res.errMsg == "requestPayment:ok") {
                  setTimeout(() => {
                    r.setData({
                      pageIndex:1
                    })
                   r.initData();  
                  }, 2000);
                }
                return;
              }
            });
           }
         }
      }
    })
    console.log(e)
  }

})